package cn.jd.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.jd.pojo.Room;
import cn.jd.service.IRoomService;

@Controller
public class RoomController {
	@Autowired
	private IRoomService roomService;
	
	@RequestMapping("/addRoom")
	
	public ModelAndView addRoom(MultipartFile roomImage,HttpServletRequest request,Room room) throws IllegalStateException, IOException{
		System.out.println(roomImage.getSize());
		//获取项目在服务器中的绝对路径
		System.out.println(request.getServletContext().getRealPath(""));
		//准备上传文件存储文件夹
		String filePath=request.getServletContext().getRealPath("")+"/uploadFile";
		File file=new File(filePath);
		if(!file.exists()){
			file.mkdir();//如果uploadFile不存在则创建
		}
		
		//获取上传文件的文件名
		String name=System.currentTimeMillis()+roomImage.getOriginalFilename();
		System.out.println(name);
		//拼接上传文件的全路径
		String fileName=filePath+"/"+name;
		System.out.println(fileName);
		
		roomImage.transferTo(new File(fileName));
		room.setImage(name);
		roomService.addRoom(room);
		
		return list();
	}
	
	@RequestMapping("/orderlist")
	public ModelAndView list(){
		List<Room> shopList = roomService.shopList();
		
		System.out.println(shopList.size());
		//实例化一个modelAndView
		ModelAndView model=new ModelAndView();
		model.addObject("shops", shopList);
		model.setViewName("/orderlist");
		return model;
	}
	
	@RequestMapping("/shoplist")
	public ModelAndView list1(){
		List<Room> shopList = roomService.shopList();
		
		System.out.println(shopList.size());
		//实例化一个modelAndView
		ModelAndView model=new ModelAndView();
		model.addObject("shops", shopList);
		model.setViewName("/shoplist");
		return model;
	}
	
	
	@RequestMapping("/del")
	public ModelAndView delShop(int id){
		System.out.println(id);
		roomService.delShop(id);
		return list();
	}
	
	@RequestMapping("/update")
	public ModelAndView updateShop(MultipartFile roomImage,HttpServletRequest request,Room room) throws IllegalStateException, IOException{
		if (roomImage.getSize()>0) {
			String filePath=request.getServletContext().getRealPath("")+"/uploadFile";
			File file=new File(filePath);
			if(!file.exists()){
				file.mkdir();//如果uploadFile不存在则创建
			}
			
			//获取上传文件的文件名
			String name=System.currentTimeMillis()+roomImage.getOriginalFilename();
			System.out.println(name);
			//拼接上传文件的全路径
			String fileName=filePath+"/"+name;
			System.out.println(fileName);
			
			roomImage.transferTo(new File(fileName));
			room.setImage(name);
		}
		roomService.updateShop(room);
		return list();
		
	}
	
	@RequestMapping("/findByConditionServlet")
	public ModelAndView searchRoom(HttpServletRequest request){
		System.out.println("断点");
		String name=request.getParameter("name");
		System.out.println(name);
		List<Room> shopList =roomService.findByCon(name);
		ModelAndView model=new ModelAndView();
		model.addObject("shops",shopList);
		model.setViewName("shoplist");//files/roomList.jsp
		return model;
		
	}

}
